package com.wectrls.service.impl;

import com.wectrls.entity.Category;
import com.wectrls.entity.Sources;
import com.wectrls.entity.WecsUser;
import com.wectrls.service.*;
import com.wectrls.vo.dto.CategoryDto;
import com.wectrls.vo.dto.PageData;
import com.wectrls.vo.queryvo.CategoryUpdateVo;
import com.wectrls.vo.queryvo.SourcesUpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2019-10-31 13:57
 */
@Service
@Transactional
public class PageServiceImpl implements PageService {

    @Autowired
    private WecsUserService wecsUserServiceImpl;

    @Autowired
    private UserGroupService userGroupServiceImpl;

    @Autowired
    private CategoryService categoryServiceImpl;

    @Autowired
    private SourcesService sourcesServiceImpl;

    @Override
    public PageData getPageData() {
        //返回数据体
        PageData pageData = new PageData();
        WecsUser wecsUser = wecsUserServiceImpl.findBySecurityUserName();
        pageData.setWecsUser(wecsUser);
        pageData.setUserGroup(userGroupServiceImpl.findById(wecsUser.getUserGroupId()));
        List<CategoryDto> categoryDtoList = categoryServiceImpl.findDtoByUserId(wecsUser.getId());
        pageData.setCategories(categoryDtoList);
        pageData.setCategoriesCount(categoryDtoList.size());
        return pageData;
    }

    @Override
    public Category updateCategory(CategoryUpdateVo categoryUpdateVo) {
        Category category = categoryServiceImpl.findById(categoryUpdateVo.getCategoryId());
        category.setName(categoryUpdateVo.getCategoryName());
        category.setSortNum(categoryUpdateVo.getSortNum());
        Category categoryForUpdate = categoryServiceImpl.saveOrUpdate(category);
        return categoryForUpdate;
    }

    @Override
    public Sources updateSources(SourcesUpdateVo sourcesUpdateVo) {
        Sources sources = sourcesServiceImpl.findById(sourcesUpdateVo.getId());
        sources.setSourcesDesc(sourcesUpdateVo.getSourcesDesc());
        sources.setSortNum(sourcesUpdateVo.getSortNum());
        sources.setSourcesName(sourcesUpdateVo.getSourcesName());
        sources.setSourcesUrl(sourcesUpdateVo.getSourcesUrl());
        Sources save = sourcesServiceImpl.saveOrUpdate(sources);
        return save;
    }

    @Override
    public Category delCategory(String categoryId) {
        Category category = categoryServiceImpl.delCategoryById(categoryId);
        return category;
    }
}
